<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\admin\model\Custom;
use app\admin\model\Order;
use app\admin\model\Yuyue;
use app\admin\model\CtmCollect;
use app\admin\model\StaffCtm;
use app\admin\model\StaffNote;
use app\admin\model\StaffLink;
use app\admin\model\Store;
use app\admin\model\CtmSpecial;
use app\admin\model\CtmCun;
use app\admin\model\CtmFlow;
use app\admin\model\MedicalAddress;
use app\admin\model\Staff;
use app\admin\model\ProvCityArea;

class CustomController extends CommonController {
    /*
     * 用户列表
     * author xfwed 
     * date 2019.09.16
     */

    public function customList() {
        $vipStatus = input('param.vip_status');
        $searchStr = input('param.search_str');
        $is_smallshop = input('param.is_smallshop');
        $smallshop_type = input('param.smallshop_type');
        $perPage = input('param.perPage');
        $page = input('param.page');
        $warning_level = input('param.warning_level');//预警等级-1全部 0正常  1死亡预警  2黑色预警 3红色预警 4黄色预警
        $start_time = input('param.start_time');
        $end_time = input('param.end_time');
        $exclusive_store_id = input('param.exclusive_store_id');//专属店铺	
        $exclusive_staff_id = input('param.exclusive_staff_id');//专属美容师
        $store_type = input('param.store_type');//店铺类型 0全部 1品牌连锁店 2服务授权店	
        $staff_type = input('param.staff_type');//美容师类型 0全部 1品牌连锁店 2服务授权店

        $vipStatus = isset($vipStatus) ? $vipStatus : 0;
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        $searchStr = trim($searchStr);
        if ($vipStatus) {
            $where['vip_status'] = $vipStatus;
        }
        if ($is_smallshop) {
            $where['is_smallshop'] = $is_smallshop;
        }
        if (isset($smallshop_type)) {
            $where['smallshop_type'] = $smallshop_type;
        }
        if(preg_match("/^[0-9][0-9]*$/" ,$warning_level)){
            $where['warning_level'] = $warning_level;
        }
        if($start_time && $end_time){
            $where['add_time'] = ['between',[$start_time,$end_time]];
        }
        if($store_type == 1){
            $ids = Store::where('type',1)->column('id');
            $where['exclusive_store_id'] = ['in',$ids];
        }elseif ($store_type == 2) {
            $ids = Store::where('type',2)->column('id');
            $where['exclusive_store_id'] = ['in',$ids];
        }

        if($staff_type == 1){
            $ids = Staff::where('type',1)->column('id');
            $where['exclusive_staff_id'] = ['in',$ids];
        }elseif ($staff_type == 2) {
            $ids = Staff::where('type',2)->column('id');
            $where['exclusive_staff_id'] = ['in',$ids];
        }
        if($exclusive_store_id){
            $where['exclusive_store_id'] = $exclusive_store_id;
        }
        if($exclusive_staff_id){
            $where['exclusive_staff_id'] = $exclusive_staff_id;
        }
        
        //排序 1.注册时间新->旧2.注册时间旧->新3.开通特权时间新->旧4.开通特权时间旧->新5.消费高->低6.消费低->高
        //7.服务次数多->少8.服务次数少->多9.美容金高->低10.美容金低->高11.最近消费新->旧12.最近消费旧->新
        //13.进店次数多->少14.进店次数少->多15.预警等级高->低16.预警等级低->高
        $orderType = input('param.orderType');
        $order = 'id desc';
        switch ($orderType) {
            case 1 :
                $order = 'add_time desc, id desc';
                break;
            case 2 :
                $order = 'add_time asc, id desc';
                break;
            case 3 :
                $order = 'special_time desc, id desc';
                break;
            case 4 :
                $order = 'special_time asc,id desc';
                break;
            case 5 :
                $order = 'consume_money desc, id desc';
                break;
            case 6 :
                $order = 'consume_money asc, id desc';
                break;
            case 7 :
                $order = 'ser_num desc, id desc';
                break;
            case 8 :
                $order = 'ser_num asc, id desc';
                break;
            case 9 :
                $order = 'red_money desc, id desc';
                break;
            case 10 :
                $order = 'red_money asc, id desc';
                break;
            case 11 :
                $order = 'sell_time desc, id desc';
                break;
            case 12 :
                $order = 'sell_time asc, id desc';
                break;
            case 13 :
                $order = 'visit_num desc, id desc';
                break;
            case 14 :
                $order = 'visit_num asc, id desc';
                break;
            case 15 :
                $order = 'warning_level=0 asc,warning_level asc, id desc';
                break;
            case 16 :
                $order = 'warning_level=0 desc,warning_level desc, id desc';
                break;
            default :
                $order = 'id desc';
        }
        
        $where = isset($where) ? $where : [];
        if ($searchStr) {
            $customList = Custom::where($where)
                    ->where(function ($query) use ($searchStr) {
                        $query->where('custom_name', 'like', '%' . $searchStr . '%')
                        ->whereOr('custom_phone', 'like', '%' . $searchStr . '%');
                    })
                    ->field('id,custom_name,custom_phone,vip_status,consume_money,real_money,tx_money,add_time,longitude,latitude,warning_level,exclusive_store_id,exclusive_staff_id,ser_num,sell_time,visit_num,red_money')
                    ->orderRaw($order)
                    ->paginate($perPage);
        } else {
            $customList = Custom::where($where)
                    ->field('id,custom_name,custom_phone,vip_status,consume_money,real_money,tx_money,add_time,longitude,latitude,warning_level,exclusive_store_id,exclusive_staff_id,ser_num,sell_time,visit_num,red_money')
                    ->orderRaw($order)
                    ->paginate($perPage);
        }
        foreach ($customList as $k => $v) {
            //用户消费金额
            $payMoney = Order::where('ctm_id', '=', $v['id'])
                    ->where('status', '=', 2)
                    ->sum('pay_money');
            $refund = Yuyue::where('ctm_id',$v['id'])->where('status',6)->sum('money');
            //$customList[$k]['consume_money'] = $payMoney - $refund;
            $customList[$k]['refund_money'] = $refund;
            //查询用户的订单数
            $customList[$k]['orderNum'] = Order::where('ctm_id', '=', $v['id'])
                    ->where('status', '=', 2)
                    ->count();
            //用户消费次数
            $customList[$k]['serNum'] = Yuyue::where('ctm_id', '=', $v['id'])
                    ->where('status', 'in', '4,7,8')
                    ->count();
            //用户消费门店
            $customList[$k]['serStoreNum'] = Yuyue::where('ctm_id', '=', $v['id'])
                    ->where('status', 'in', '4,7,8')
                    ->group('store_id')
                    ->count();
            //关注的美容师
            $customList[$k]['collectStaff'] = CtmCollect::where('ctm_id', '=', $v['id'])
                    ->where('type', '=', 2)
                    ->count();
            //收藏的项目
            $customList[$k]['collectItem'] = CtmCollect::where('ctm_id', '=', $v['id'])
                    ->where('type', '=', 1)
                    ->count();
            //查询用户的充值金额
            $customList[$k]['payMoney'] = Order::where('ctm_id', '=', $v['id'])
                    ->where('status', '=', 2)
                    ->where('type', '=', 3)
                    ->sum('pay_money');
            //查询用户开通特权的时间
            if ($v['vip_status'] == 2 || $v['vip_status'] == 3) {
                $customList[$k]['special_time'] = CtmSpecial::where('ctm_id', $v['id'])->value('add_time');
            }
            //查询用户的专属店铺
            $exclusive_store_id = $v['exclusive_store_id'];
            $customList[$k]['exclusive_store_name'] = Store::where('id',$exclusive_store_id)->value('store_name');
            $customList[$k]['store_type'] = Store::where('id',$exclusive_store_id)->value('type');
            //查询用户的专属美容师
            $exclusive_staff_id = $v['exclusive_staff_id'];
            $customList[$k]['exclusive_staff_name'] = Staff::where('id',$exclusive_staff_id)->value('staff_name');
            $customList[$k]['staff_type'] = Staff::where('id',$exclusive_staff_id)->value('type');
        }

        return $this->ajaxReturn(0, '查询成功', $customList);
    }

    /*
     * 用户详情
     * author xfwed 
     * date 2019.09.17
     */

    public function customInfo() {
        $id = input('param.id');

        $customList = Custom::where('id', '=', $id)
                ->field('id,custom_photo,custom_name,custom_phone,vip_status,consume_money,real_money,tx_money,red_money,add_time,birthday,is_smallshop,smallshop_storeid,idcard1,idcard2,cardnum')
                ->find();
        $customList['custom_sex'] = 2;
        $customList['height'] = '未知';
        $customList['weight'] = '未知';
        $customList['health'] = rand(60, 99);
        $customList['skin'] = [];
        $customList['fat'] = [];
        //查询用户的订单数
        $customList['orderNum'] = Order::where('ctm_id', '=', $customList['id'])
                ->where('status', '=', 2)
                ->count();

        //关注的美容师
        $customList['collectStaff'] = CtmCollect::where('ctm_id', '=', $customList['id'])
                ->where('type', '=', 2)
                ->count();
        //收藏的项目
        $customList['collectItem'] = CtmCollect::where('ctm_id', '=', $customList['id'])
                ->where('type', '=', 1)
                ->count();
        //喜欢的店铺
        $customList['collectStore'] = CtmCollect::where('ctm_id', '=', $customList['id'])
                ->where('type', '=', 3)
                ->count();
        //查询用户的充值金额
        $customList['payMoney'] = Order::where('ctm_id', '=', $customList['id'])
                ->where('status', '=', 2)
                ->where('type', '=', 3)
                ->sum('pay_money');
        //用户代金券数
        $customList['cunNum'] = CtmCun::where('ctm_id', $customList['id'])->where('employ', 2)->count();
        //用户服务技师
        $customList['staff'] = Yuyue::alias('y')
                ->join('staff s', 's.id = y.staff_id', 'left')
                ->where('y.ctm_id', $customList['id'])
                ->where('y.status', '4,7,8')
                ->value('s.staff_name');
        //用户小店分瑞的店铺
        $customList['storeName'] = Store::where('id',$customList['smallshop_storeid'])->value('store_name');
        //用户小店收益
        $customList['smallStoreIncome'] = CtmFlow::where('ctm_id',$id)->where('pro_type','in','8,9')->sum('money');

        return $this->ajaxReturn(0, '查询成功', $customList);
    }

    /*
     * 我的推广顾客
     * author xfwed
     * date 2019.11.12
     */

//    public function myCtm() {
//        $rule = [
//            'staff_id' => 'require',
//            'type' => 'require',
//            'perPage' => 'integer|egt:1',
//            'page' => 'integer|egt:1',
//        ];
//        $msg = [
//            'type.require' => '分类必须',
//            'staff_id.require' => '美容师id必须',
//            'perPage.integer' => '每页数量为数字',
//            'perPage.egt' => '每页数量最小为1',
//            'page.integer' => '当前页数为数字',
//            'page.egt' => '当前页数最小为1',
//        ];
//
//        $all = input('param.');
//
//        $validate = new Validate($rule, $msg);
//        if (!$validate->check($all)) {
//            $error = $validate->getError();
//
//            return $this->ajaxReturn(1, $error, null);
//        }
//
//        $type = $all['type']; ////1未消费2新客户3老客户4开通特权5粉丝6体验特权7开通i美优优
//        $staff_id = $all['staff_id'];
//        $store_id = input('param.store_id');
//        $tab = input('param.tab');
//        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
//        $page = isset($all['page']) ? $all['page'] : 1;
//
//
//        if ($store_id) {
//            $where = "(SELECT COUNT(`id`) FROM `ai_yuyue` WHERE `status` IN(4,7,8) and `store_id`=" . $store_id . " and `order_id`=o.id) > 0";
//            $where1 = "(SELECT COUNT(`id`) FROM `ai_yuyue` WHERE `status` IN(4,7,8) and `store_id`=" . $store_id . " and `order_id`=y.order_id) > 0";
//        } else {
//            $where = [];
//            $where1 = [];
//        }
//        //tab数量
//        $noExpense = StaffCtm::alias('s')
//                ->where('s.staff_id', $staff_id)
//                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
//                ->group('s.ctm_id')
//                ->count();
//        $data['noExpense'] = $noExpense;
//        $new = Order::alias('o')
//                ->join('yuyue y','y.order_id = o.id','left')
//                ->where('o.staff_id', $staff_id)
//                ->where('o.status', 2)
//                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
//                ->where('y.status','in','1,2,3,4,5,7,8,9,10,11')
//                ->where($where)
//                ->group('o.ctm_id')
//                ->count();
//        $data['newNum'] = $new;
//        $old = Order::alias('o')
//                ->where('o.staff_id', $staff_id)
//                ->where('o.status', 2)
//                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) > 1")
//                ->where($where)
//                ->group('o.ctm_id')
//                ->count();
//        $data['old'] = $old;
//        $special1 = Order::where('staff_id', $staff_id)//本店开通
//                ->where('status', 2)
//                ->where('type', 4)
//                ->where('pay_time', '>', date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 365))
//                ->where('mini_shop_id',0)
//                ->group('ctm_id')
//                ->count();
//        $special2 = StaffCtm::alias('s')//逆行者活动
//                ->join('custom c', 'c.id = s.ctm_id','left')
//                ->join('ctm_special cs','cs.ctm_id=s.ctm_id','left')
//                ->where('s.staff_id', $staff_id)
//                ->where('c.vip_status', 2)
//                ->where('cs.type',3)
//                ->group('s.ctm_id')
//                ->count();
//        $special3 = Order::where('staff_id', $staff_id)//小店转介绍
//                ->where('status', 2)
//                ->where('type', 4)
//                ->where('pay_time', '>', date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 365))
//                ->where('mini_shop_id','<>',0)
//                ->group('ctm_id')
//                ->count();
//        $data['special1'] = $special1;
//        $data['special2'] = $special2;
//        $data['special3'] = $special3;
//        $data['special'] = $special1+$special2+$special3;
//        $fans = CtmCollect::where('type', 2)
//                ->where('pro_id', $staff_id)
//                ->count();
//        $data['fans'] = $fans;
//        $expSpecial = StaffCtm::alias('s')
//                ->join('custom c', 'c.id = s.ctm_id')
//                ->where('s.staff_id', $staff_id)
//                //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
//                ->where('c.vip_status', 3)
//                ->group('s.ctm_id')
//                ->count();
//        $data['expSpecial'] = $expSpecial;
//        $smallshop = StaffCtm::alias('s')
//                ->join('custom c', 'c.id = s.ctm_id')
//                ->where('s.staff_id', $staff_id)
//                //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
//                ->where('c.is_smallshop', 1)
//                ->group('s.ctm_id')
//                ->count();
//        $data['smallshop'] = $smallshop;
//        if ($type == 2) {
//            //全部
//            $all = Order::alias('o')
//                    ->join('yuyue y','y.order_id = o.id','left')
//                    ->where('o.staff_id', $staff_id)
//                    ->where('o.status', 2)
//                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
//                    ->where('y.status','in','1,2,3,4,5,7,8,9,10,11')
//                    ->where($where)
//                    ->group('o.ctm_id')
//                    ->count();
//            $data['all'] = $all;
//            //已付款
//            $pay = Order::alias('o')
//                    ->join('yuyue y','y.order_id = o.id','left')
//                    ->where('o.staff_id', $staff_id)
//                    ->where('o.status', 2)
//                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
//                    ->where('y.status','in','1,5,9,10,11')
//                    ->where($where)
//                    ->group('o.ctm_id')
//                    ->count();
////            return Utils::ajaxReturn(0, '666', $pay);
//
//
//            $data['pay'] = $pay;
//            //预约中
//            $yuyue = Order::alias('o')
//                    ->join('yuyue y','y.order_id = o.id','left')
//                    ->where('o.staff_id', $staff_id)
//                    ->where('o.status', 2)
//                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
//                    ->where('y.status','in','2,3')
//                    ->where($where)
//                    ->group('o.ctm_id')
//                    ->count();
//            $data['yuuyue'] = $yuyue;
//            //服务完成
//            $fuwu = Order::alias('o')
//                    ->join('yuyue y','y.order_id = o.id','left')
//                    ->where('o.staff_id', $staff_id)
//                    ->where('o.status', 2)
//                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
//                    ->where('y.status','in','4,7,8')
//                    ->where($where)
//                    ->group('o.ctm_id')
//                    ->count();
//            $data['fuwu'] = $fuwu;
//        }
//
//
//
//
//        //列表   1未消费2新客户3老客户4开通特权5粉丝
//        //列表   1未消费2新客户3老客户4开通特权5粉丝6体验特权7开通i美优优
//        if ($type == 1) {
//            $noExpenseList = StaffCtm::alias('s')
//                    ->where('s.staff_id', $staff_id)
//                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
//                    ->group('s.ctm_id')
//                    ->order('s.add_time desc')
//                    ->field('s.ctm_id')
//                    ->paginate($perPage);
//            foreach ($noExpenseList as $k => $v) {
//                $ctmRow = Custom::where('id', $v['ctm_id'])
//                        ->field('id,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
//                        ->find();
//                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
//                $noExpenseList[$k]['note'] = $notename['note'];
//                $noExpenseList[$k]['note_name'] = $notename['name'];
//                if ($notename['name']) {
//                    $ctmRow['custom_name'] = $notename['name'];
//                }
//                //用户是否关注了美容师
//                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
//                        ->where('type', 2)
//                        ->where('pro_id', $staff_id)
//                        ->find();
//                if ($ctmCollectRow) {
//                    $noExpenseList[$k]['is_focus'] = 1; //1是2否关注了我
//                } else {
//                    $noExpenseList[$k]['is_focus'] = 2;
//                }
//                //沟通信息
//                $noExpenseList[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->order('add_time desc')
//                        ->value('add_time');
//
//                //是否是通过美容师开通的特权
//                $orderRow = Order::where('type', 4)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->where('status', 2)
//                        ->where('staff_id', $staff_id)
//                        ->find();
//                if ($orderRow) {
//                    $noExpenseList[$k]['special'] = 1; //1是2否通过我购买的
//                } else {
//                    $noExpenseList[$k]['special'] = 2;
//                }
//                //查询用户开通特权时间
//                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
//                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
//                    $noExpenseList[$k]['specialTime'] = $specialTime['add_time'];
//                    if($specialTime['type'] == 3){
//                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
//                    }  else {
//                        $specialOrder = Order::alias('o')
//                            ->where('o.ctm_id', '=', $ctmRow['id'])
//                            ->where('o.status', 2)
//                            ->where('o.type', 4)
//                            ->where('o.mini_shop_id','<>',0)
//                            ->group('ctm_id')
//                            ->count();
//                        if($specialOrder > 0){
//                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
//                        }
//                    }
//                } else {
//                    $noExpenseList[$k]['specialTime'] = NULL;
//                }
//                //查询用户是否为逆行者
//                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
//                if($medicalData > 0){
//                    $is_medical = 1;
//                }  else {
//                    $is_medical = 2;
//                }
//                $noExpenseList[$k]['is_medical'] = $is_medical;
//                $noExpenseList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
//                
//                $noExpenseList[$k]['custom_phone'] = $ctmRow['custom_phone'];
//                $noExpenseList[$k]['custom_name'] = $ctmRow['custom_name'];
//                $noExpenseList[$k]['custom_photo'] = $ctmRow['custom_photo'];
//                $noExpenseList[$k]['vip_status'] = $ctmRow['vip_status'];
//                $noExpenseList[$k]['source_type'] = $ctmRow['source_type'];
//                $noExpenseList[$k]['source_id'] = $ctmRow['source_id'];
//            }
//            $data['list'] = $noExpenseList;
//        } elseif ($type == 2) {
//            if ($tab == 1) {
//                $yuyuewhere['y.status'] = ['in','1,2,3,4,5,7,8,9,10,11'];
//            } elseif ($tab == 2) {
//                $yuyuewhere['y.status'] = ['in','1,5,9,10,11'];
//            } elseif ($tab == 3) {
//                $yuyuewhere['y.status'] = ['in','2,3'];
//            } elseif ($tab == 4) {
//                $yuyuewhere['y.status'] = ['in','4,7,8'];
//            }
//
//            $allList = Order::alias('o')
//                    ->join('yuyue y','y.order_id = o.id','left')
//                    ->where('o.staff_id', $staff_id)
//                    ->where('o.status', 2)
//                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
//                    ->where($yuyuewhere)
//                    ->where($where)
//                    ->group('o.ctm_id')
//                    ->order('o.add_time desc')
//                    ->field('o.id,o.ctm_id')
//                    ->paginate($perPage);
//            foreach ($allList as $k => $v) {
//                $ctmRow = Custom::where('id', $v['ctm_id'])
//                        ->field('id,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
//                        ->find();
//                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
//                $allList[$k]['note'] = $notename['note'];
//                $allList[$k]['note_name'] = $notename['name'];
//                if ($notename['name']) {
//                    $ctmRow['custom_name'] = $notename['name'];
//                }
//                //用户是否关注了美容师
//                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
//                        ->where('type', 2)
//                        ->where('pro_id', $staff_id)
//                        ->find();
//                if ($ctmCollectRow) {
//                    $allList[$k]['is_focus'] = 1; //1是2否关注了我
//                } else {
//                    $allList[$k]['is_focus'] = 2;
//                }
//                //沟通信息
//                $allList[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->order('add_time desc')
//                        ->value('add_time');
//
//                //是否是通过美容师开通的特权
//                $orderRow = Order::where('type', 4)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->where('status', 2)
//                        ->where('staff_id', $staff_id)
//                        ->find();
//                if ($orderRow) {
//                    $allList[$k]['special'] = 1; //1是2否通过我购买的
//                } else {
//                    $allList[$k]['special'] = 2;
//                }
//                //查询用户开通特权时间
//                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
//                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
//                    $allList[$k]['specialTime'] = $specialTime['add_time'];
//                    if($specialTime['type'] == 3){
//                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
//                    }  else {
//                        $specialOrder = Order::alias('o')
//                            ->where('o.ctm_id', '=', $ctmRow['id'])
//                            ->where('o.status', 2)
//                            ->where('o.type', 4)
//                            ->where('o.mini_shop_id','<>',0)
//                            ->group('ctm_id')
//                            ->count();
//                        if($specialOrder > 0){
//                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
//                        }
//                    }
//                } else {
//                    $allList[$k]['specialTime'] = NULL;
//                }
//                //查询用户是否为逆行者
//                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
//                if($medicalData > 0){
//                    $is_medical = 1;
//                }  else {
//                    $is_medical = 2;
//                }
//                $allList[$k]['is_medical'] = $is_medical;
//                $allList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
//                
//                $allList[$k]['custom_phone'] = $ctmRow['custom_phone'];
//                $allList[$k]['custom_name'] = $ctmRow['custom_name'];
//                $allList[$k]['custom_photo'] = $ctmRow['custom_photo'];
//                $allList[$k]['vip_status'] = $ctmRow['vip_status'];
//                $allList[$k]['source_type'] = $ctmRow['source_type'];
//                $allList[$k]['source_id'] = $ctmRow['source_id'];
//                $yuyueRow = Yuyue::where('order_id', $v['id'])
//                        ->order('status asc')
//                        ->field('id,store_id,status,start_time')
//                        ->find();
//                $allList[$k]['yuyue_status'] = $yuyueRow['status'];
//                $allList[$k]['yuyue_start_time'] = $yuyueRow['start_time'];
//                if ($yuyueRow['store_id'] != 0) {
//                    $allList[$k]['store_name'] = Store::where('id', $yuyueRow['store_id'])->value('store_name');
//                }
//            }
//            $data['list'] = $allList;
//        } elseif ($type == 3) {
//            $oldList = Order::alias('o')
//                    ->where('o.staff_id', $staff_id)
//                    ->where('o.status', 2)
//                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) > 1")
//                    ->where($where)
//                    ->group('o.ctm_id')
//                    ->order('o.add_time desc')
//                    ->field('o.id,o.ctm_id')
//                    ->paginate($perPage);
//            foreach ($oldList as $k => $v) {
//                $ctmRow = Custom::where('id', $v['ctm_id'])
//                        ->field('id,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
//                        ->find();
//                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
//                $oldList[$k]['note'] = $notename['note'];
//                $oldList[$k]['note_name'] = $notename['name'];
//                if ($notename['name']) {
//                    $ctmRow['custom_name'] = $notename['name'];
//                }
//                //用户是否关注了美容师
//                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
//                        ->where('type', 2)
//                        ->where('pro_id', $staff_id)
//                        ->find();
//                if ($ctmCollectRow) {
//                    $oldList[$k]['is_focus'] = 1; //1是2否关注了我
//                } else {
//                    $oldList[$k]['is_focus'] = 2;
//                }
//                //沟通信息
//                $oldList[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->order('add_time desc')
//                        ->value('add_time');
//
//                //是否是通过美容师开通的特权
//                $orderRow = Order::where('type', 4)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->where('status', 2)
//                        ->where('staff_id', $staff_id)
//                        ->find();
//                if ($orderRow) {
//                    $oldList[$k]['special'] = 1; //1是2否通过我购买的
//                } else {
//                    $oldList[$k]['special'] = 2;
//                }
//                //查询用户开通特权时间
//                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
//                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
//                    $oldList[$k]['specialTime'] = $specialTime['add_time'];
//                    if($specialTime['type'] == 3){
//                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
//                    }  else {
//                        $specialOrder = Order::alias('o')
//                            ->where('o.ctm_id', '=', $ctmRow['id'])
//                            ->where('o.status', 2)
//                            ->where('o.type', 4)
//                            ->where('o.mini_shop_id','<>',0)
//                            ->group('ctm_id')
//                            ->count();
//                        if($specialOrder > 0){
//                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
//                        }
//                    }
//                } else {
//                    $oldList[$k]['specialTime'] = NULL;
//                }
//                //查询用户是否为逆行者
//                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
//                if($medicalData > 0){
//                    $is_medical = 1;
//                }  else {
//                    $is_medical = 2;
//                }
//                $oldList[$k]['is_medical'] = $is_medical;
//                $oldList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
//                
//                $oldList[$k]['custom_phone'] = $ctmRow['custom_phone'];
//                $oldList[$k]['custom_name'] = $ctmRow['custom_name'];
//                $oldList[$k]['custom_photo'] = $ctmRow['custom_photo'];
//                $oldList[$k]['vip_status'] = $ctmRow['vip_status'];
//                $oldList[$k]['source_type'] = $ctmRow['source_type'];
//                $oldList[$k]['source_id'] = $ctmRow['source_id'];
//                $yuyueRow = Yuyue::where('order_id', $v['id'])
//                        ->order('status asc')
//                        ->field('id,store_id,status,start_time')
//                        ->find();
//                $oldList[$k]['yuyue_status'] = $yuyueRow['status'];
//                $oldList[$k]['yuyue_start_time'] = $yuyueRow['start_time'];
//                if ($yuyueRow['store_id'] != 0) {
//                    $oldList[$k]['store_name'] = Store::where('id', $yuyueRow['store_id'])->value('store_name');
//                }
//            }
//            $data['list'] = $oldList;
//        } elseif ($type == 4) {
//            if($tab == 1){//1本店开通2逆行者活动3小店转介绍
//                $specialList = Order::where('staff_id', $staff_id)
//                    ->where('status', 2)
//                    ->where('type', 4)
//                    ->where('pay_time', '>', date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 365))
//                    ->where('mini_shop_id',0)
//                    ->group('ctm_id')
//                    ->order('add_time desc')
//                    ->field('id,ctm_id')
//                    ->paginate($perPage);
//            }elseif ($tab == 2) {
//                $specialList = StaffCtm::alias('s')//逆行者活动
//                        ->join('custom c', 'c.id = s.ctm_id','left')
//                        ->join('ctm_special cs','cs.ctm_id=s.ctm_id','left')
//                        ->where('s.staff_id', $staff_id)
//                        ->where('c.vip_status', 2)
//                        ->where('cs.type',3)
//                        ->group('s.ctm_id')
//                        ->order('s.add_time desc')
//                        ->field('s.id,s.ctm_id')
//                        ->paginate($perPage);
//            }elseif ($tab == 3) {
//                $specialList = Order::where('staff_id', $staff_id)
//                    ->where('status', 2)
//                    ->where('type', 4)
//                    ->where('pay_time', '>', date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 365))
//                    ->where('mini_shop_id','<>',0)
//                    ->group('ctm_id')
//                    ->order('o.add_time desc')
//                    ->field('id,ctm_id')
//                    ->paginate($perPage);
//            }
//            
//            
//            foreach ($specialList as $k => $v) {
//                $ctmRow = Custom::where('id', $v['ctm_id'])
//                        ->field('id,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
//                        ->find();
//                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
//                $specialList[$k]['note'] = $notename['note'];
//                $specialList[$k]['note_name'] = $notename['name'];
//                if ($notename['name']) {
//                    $ctmRow['custom_name'] = $notename['name'];
//                }
//                //用户是否关注了美容师
//                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
//                        ->where('type', 2)
//                        ->where('pro_id', $staff_id)
//                        ->find();
//                if ($ctmCollectRow) {
//                    $specialList[$k]['is_focus'] = 1; //1是2否关注了我
//                } else {
//                    $specialList[$k]['is_focus'] = 2;
//                }
//                //沟通信息
//                $specialList[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->order('add_time desc')
//                        ->value('add_time');
//
//                //是否是通过美容师开通的特权
//                $orderRow = Order::where('type', 4)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->where('status', 2)
//                        ->where('staff_id', $staff_id)
//                        ->find();
//                if ($orderRow) {
//                    $specialList[$k]['special'] = 1; //1是2否通过我购买的
//                } else {
//                    $specialList[$k]['special'] = 2;
//                }
//                //查询用户开通特权时间
//                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
//                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
//                    $specialList[$k]['specialTime'] = $specialTime['add_time'];
//                    if($specialTime['type'] == 3){
//                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
//                    }  else {
//                        $specialOrder = Order::alias('o')
//                            ->where('o.ctm_id', '=', $ctmRow['id'])
//                            ->where('o.status', 2)
//                            ->where('o.type', 4)
//                            ->where('o.mini_shop_id','<>',0)
//                            ->group('ctm_id')
//                            ->count();
//                        if($specialOrder > 0){
//                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
//                        }
//                    }
//                } else {
//                    $specialList[$k]['specialTime'] = NULL;
//                }
//                //查询用户是否为逆行者
//                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
//                if($medicalData > 0){
//                    $is_medical = 1;
//                }  else {
//                    $is_medical = 2;
//                }
//                $specialList[$k]['is_medical'] = $is_medical;
//                $specialList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
//                
//                $specialList[$k]['custom_phone'] = $ctmRow['custom_phone'];
//                $specialList[$k]['custom_name'] = $ctmRow['custom_name'];
//                $specialList[$k]['custom_photo'] = $ctmRow['custom_photo'];
//                $specialList[$k]['vip_status'] = $ctmRow['vip_status'];
//                $specialList[$k]['source_type'] = $ctmRow['source_type'];
//                $specialList[$k]['source_id'] = $ctmRow['source_id'];
//            }
//
//            $data['list'] = $specialList;
//        } elseif ($type == 5) {
//            $fansList = CtmCollect::where('type', 2)
//                    ->where('pro_id', $staff_id)
//                    ->order('add_time desc')
//                    ->field('id,ctm_id')
//                    ->paginate($perPage);
//            foreach ($fansList as $k => $v) {
//                $ctmRow = Custom::where('id', $v['ctm_id'])
//                        ->field('id,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
//                        ->find();
//                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
//                $fansList[$k]['note'] = $notename['note'];
//                $fansList[$k]['note_name'] = $notename['name'];
//                if ($notename['name']) {
//                    $ctmRow['custom_name'] = $notename['name'];
//                }
//                //用户是否关注了美容师
//                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
//                        ->where('type', 2)
//                        ->where('pro_id', $staff_id)
//                        ->find();
//                if ($ctmCollectRow) {
//                    $fansList[$k]['is_focus'] = 1; //1是2否关注了我
//                } else {
//                    $fansList[$k]['is_focus'] = 2;
//                }
//                //沟通信息
//                $fansList[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->order('add_time desc')
//                        ->value('add_time');
//
//                //是否是通过美容师开通的特权
//                $orderRow = Order::where('type', 4)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->where('status', 2)
//                        ->where('staff_id', $staff_id)
//                        ->find();
//                if ($orderRow) {
//                    $fansList[$k]['special'] = 1; //1是2否通过我购买的
//                } else {
//                    $fansList[$k]['special'] = 2;
//                }
//                //查询用户开通特权时间
//                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
//                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
//                    $fansList[$k]['specialTime'] = $specialTime['add_time'];
//                    if($specialTime['type'] == 3){
//                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
//                    }  else {
//                        $specialOrder = Order::alias('o')
//                            ->where('o.ctm_id', '=', $ctmRow['id'])
//                            ->where('o.status', 2)
//                            ->where('o.type', 4)
//                            ->where('o.mini_shop_id','<>',0)
//                            ->group('ctm_id')
//                            ->count();
//                        if($specialOrder > 0){
//                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
//                        }
//                    }
//                } else {
//                    $fansList[$k]['specialTime'] = NULL;
//                }
//                //查询用户是否为逆行者
//                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
//                if($medicalData > 0){
//                    $is_medical = 1;
//                }  else {
//                    $is_medical = 2;
//                }
//                $fansList[$k]['is_medical'] = $is_medical;
//                $fansList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
//                
//                $fansList[$k]['custom_phone'] = $ctmRow['custom_phone'];
//                $fansList[$k]['custom_name'] = $ctmRow['custom_name'];
//                $fansList[$k]['custom_photo'] = $ctmRow['custom_photo'];
//                $fansList[$k]['vip_status'] = $ctmRow['vip_status'];
//                $fansList[$k]['source_type'] = $ctmRow['source_type'];
//                $fansList[$k]['source_id'] = $ctmRow['source_id'];
//            }
//            $data['list'] = $fansList;
//        } elseif ($type == 6) {
//            $expSpecial = StaffCtm::alias('s')
//                    ->join('custom c', 'c.id = s.ctm_id')
//                    ->where('c.vip_status', 3)
//                    ->where('s.staff_id', $staff_id)
//                    //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
//                    ->group('s.ctm_id')
//                    ->order('s.add_time desc')
//                    ->field('s.ctm_id')
//                    ->paginate($perPage);
//            foreach ($expSpecial as $k => $v) {
//                $ctmRow = Custom::where('id', $v['ctm_id'])
//                        ->field('id,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
//                        ->find();
//                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
//                $expSpecial[$k]['note'] = $notename['note'];
//                $expSpecial[$k]['note_name'] = $notename['name'];
//                if ($notename['name']) {
//                    $ctmRow['custom_name'] = $notename['name'];
//                }
//                //用户是否关注了美容师
//                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
//                        ->where('type', 2)
//                        ->where('pro_id', $staff_id)
//                        ->find();
//                if ($ctmCollectRow) {
//                    $expSpecial[$k]['is_focus'] = 1; //1是2否关注了我
//                } else {
//                    $expSpecial[$k]['is_focus'] = 2;
//                }
//                //沟通信息
//                $expSpecial[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->order('add_time desc')
//                        ->value('add_time');
//
//                //是否是通过美容师开通的特权
//                $orderRow = Order::where('type', 4)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->where('status', 2)
//                        ->where('staff_id', $staff_id)
//                        ->find();
//                if ($orderRow) {
//                    $expSpecial[$k]['special'] = 1; //1是2否通过我购买的
//                } else {
//                    $expSpecial[$k]['special'] = 2;
//                }
//                //查询用户开通特权时间
//                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
//                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
//                    $expSpecial[$k]['specialTime'] = $specialTime['add_time'];
//                    if($specialTime['type'] == 3){
//                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
//                    }  else {
//                        $specialOrder = Order::alias('o')
//                            ->where('o.ctm_id', '=', $ctmRow['id'])
//                            ->where('o.status', 2)
//                            ->where('o.type', 4)
//                            ->where('o.mini_shop_id','<>',0)
//                            ->group('ctm_id')
//                            ->count();
//                        if($specialOrder > 0){
//                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
//                        }
//                    }
//                } else {
//                    $expSpecial[$k]['specialTime'] = NULL;
//                }
//                //查询用户是否为逆行者
//                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
//                if($medicalData > 0){
//                    $is_medical = 1;
//                }  else {
//                    $is_medical = 2;
//                }
//                $expSpecial[$k]['is_medical'] = $is_medical;
//                $expSpecial[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
//                
//                $expSpecial[$k]['custom_phone'] = $ctmRow['custom_phone'];
//                $expSpecial[$k]['custom_name'] = $ctmRow['custom_name'];
//                $expSpecial[$k]['custom_photo'] = $ctmRow['custom_photo'];
//                $expSpecial[$k]['vip_status'] = $ctmRow['vip_status'];
//                $expSpecial[$k]['source_type'] = $ctmRow['source_type'];
//                $expSpecial[$k]['source_id'] = $ctmRow['source_id'];
//            }
//            $data['list'] = $expSpecial;
//        }elseif ($type == 7) {
//            $smallshop = StaffCtm::alias('s')
//                    ->join('custom c', 'c.id = s.ctm_id')
//                    ->where('c.is_smallshop', 1)
//                    ->where('s.staff_id', $staff_id)
//                    //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
//                    ->group('s.ctm_id')
//                    ->order('s.add_time desc')
//                    ->field('s.ctm_id')
//                    ->paginate($perPage);
//            foreach ($smallshop as $k => $v) {
//                $ctmRow = Custom::where('id', $v['ctm_id'])
//                        ->field('id,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
//                        ->find();
//                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
//                $smallshop[$k]['note'] = $notename['note'];
//                $smallshop[$k]['note_name'] = $notename['name'];
//                if ($notename['name']) {
//                    $ctmRow['custom_name'] = $notename['name'];
//                }
//                //用户是否关注了美容师
//                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
//                        ->where('type', 2)
//                        ->where('pro_id', $staff_id)
//                        ->find();
//                if ($ctmCollectRow) {
//                    $smallshop[$k]['is_focus'] = 1; //1是2否关注了我
//                } else {
//                    $smallshop[$k]['is_focus'] = 2;
//                }
//                //沟通信息
//                $smallshop[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->order('add_time desc')
//                        ->value('add_time');
//
//                //是否是通过美容师开通的特权
//                $orderRow = Order::where('type', 4)
//                        ->where('ctm_id', $v['ctm_id'])
//                        ->where('status', 2)
//                        ->where('staff_id', $staff_id)
//                        ->find();
//                if ($orderRow) {
//                    $smallshop[$k]['special'] = 1; //1是2否通过我购买的
//                } else {
//                    $smallshop[$k]['special'] = 2;
//                }
//                //查询用户开通特权时间
//                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
//                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
//                    $smallshop[$k]['specialTime'] = $specialTime['add_time'];
//                    if($specialTime['type'] == 3){
//                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
//                    }  else {
//                        $specialOrder = Order::alias('o')
//                            ->where('o.ctm_id', '=', $ctmRow['id'])
//                            ->where('o.status', 2)
//                            ->where('o.type', 4)
//                            ->where('o.mini_shop_id','<>',0)
//                            ->group('ctm_id')
//                            ->count();
//                        if($specialOrder > 0){
//                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
//                        }
//                    }
//                } else {
//                    $smallshop[$k]['specialTime'] = NULL;
//                }
//                //查询用户是否为逆行者
//                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
//                if($medicalData > 0){
//                    $is_medical = 1;
//                }  else {
//                    $is_medical = 2;
//                }
//                $smallshop[$k]['is_medical'] = $is_medical;
//                $smallshop[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
//                
//                $smallshop[$k]['custom_phone'] = $ctmRow['custom_phone'];
//                $smallshop[$k]['custom_name'] = $ctmRow['custom_name'];
//                $smallshop[$k]['custom_photo'] = $ctmRow['custom_photo'];
//                $smallshop[$k]['vip_status'] = $ctmRow['vip_status'];
//                $smallshop[$k]['source_type'] = $ctmRow['source_type'];
//                $smallshop[$k]['source_id'] = $ctmRow['source_id'];
//            }
//            $data['list'] = $smallshop;
//        }
//        foreach ($data['list'] as $k => $v){
//            if($v['source_type'] == 0){
//                $sourceName = '不知道';
//            }elseif ($v['source_type'] == 1) {
//                $sourceName = '平台引流';
//            }elseif ($v['source_type'] == 2) {
//                $staffName = Staff::where('id',$v['source_id'])->value('staff_name');
//                $sourceName = $staffName;
//            }elseif ($v['source_type'] == 3) {
//                $StoreRow = Store::where('id',$v['source_id'])->field('store_name,city')->find();
//                $cityName = ProvCityArea::where('id',$StoreRow['city'])->value('name');
//                $sourceName = $cityName.'的'.$StoreRow['store_name'];
//            }elseif ($v['source_type'] == 4) {
//                $sourceName = '小店转介绍';
//            }
//            $data['list'][$k]['sourceName'] = $sourceName;
//        }
//        return $this->ajaxReturn(0, '成功', $data);
//    }


    /*
     * 每日访客
     * author xfwed
     * date 2019-12-13
     */

    public function callerList() {
        $type = input('param.type');
        $perPage = input('param.perPage');
        $page = input('param.page');
        $start_time = input('param.start_time');
        $end_time = input('param.end_time');
        $store_name = input('param.store_name');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($type == 1) {//0全部1新访客2会员访客
            $where['f.person_ids'] = 0;
        } elseif ($type == 2) {
            $where['f.person_ids'] = ['<>', 0];
        }
        if ($end_time && $start_time) {
            $where['f.add_time'] = array('between', array($start_time, $end_time));
        } elseif (empty($end_time) && $start_time) {
            $time = strtotime($start_time);
            $time2 = $time + 24 * 60 * 60;
            $time2 = date('Y-m-d H:i:s', $time2);
            $where['f.add_time'] = array('between', array($time, $time2));
        }
        if ($store_name) {
            $where['s.store_name'] = ['like', '%' . $store_name . '%'];
        }
        $where['ms.machine_type'] = 3;
        $list = Db::table('da_face_recognition')->alias('f')
                ->join(['da_ctm_face_relationship' => 'cf'], 'cf.person_code=f.person_ids', 'left')
                ->join(['da_machine_store_relation' => 'ms'], 'ms.robot_no=f.equno', 'left')
                ->join('store s', 's.id=ms.store_id', 'left')
                ->join('custom c', 'c.custom_phone = cf.phone', 'left')
                ->join('prov_city_area p', 'p.id = s.city', 'left')
                ->field('f.id,f.image_time,f.face_frame,c.id ctm_id,c.custom_name,c.custom_phone,c.vip_status,s.store_name,p.name')
                ->where($where)
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 用户消费订单
     * author xfwed 
     * date 2019.12.25
     */

    public function ctmOrder() {
        $ctm_id = input('param.ctm_id'); //用户id
        $type = input('param.type'); //1项目2商品
        $status = input('param.status'); //订单状态
        $mini_shop_id = input('param.mini_shop_id');//1是2否小店订单
        $start_time = input('param.start_time');
        $end_time = input('param.end_time');

        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;



        if ($type == 1) {
            if ($end_time && $start_time) {
                $where['o.pay_time'] = array('between', array($start_time, $end_time));
            } elseif (empty($end_time) && $start_time) {
                $time = strtotime($start_time);
                $time2 = $time + 24 * 60 * 60;
                $time2 = date('Y-m-d H:i:s', $time2);
                $where['o.pay_time'] = array('between', array($time, $time2));
            }


            $where = isset($where) ? $where : [];
            if ($status) {
                $where['o.status'] = $status;
            }
            if ($status == -1) {
                $where['o.status'] = 0;
            }
            if($mini_shop_id == 1){
                $where['o.mini_shop_id'] = ['<>',0];
            }elseif ($mini_shop_id == 2) {
                $where['o.mini_shop_id'] = 0;
            }
            $where['type'] = ['<>',7];
            $orderList = Order::alias('o')
                    ->join('custom c', 'c.id = o.ctm_id', 'left')
                    ->where($where)
                    ->where('o.ctm_id', $ctm_id)
                    ->field('o.id,o.ord_num,o.status,o.pay_time,o.type,o.pay_money,o.balance_money,o.red_money,o.special_id,c.custom_name,c.custom_phone,c.vip_status')
                    ->order('o.id desc')
                    ->paginate($perPage);
        } elseif ($type == 2) {
            if ($status) {
                $where['pd.status'] = $status;
            }
            if ($status == -1) {
                $where['pd.status'] = 0;
            }
            
            if($mini_shop_id == 1){
                $where['pd.mini_shop_id'] = ['<>',0];
            }elseif ($mini_shop_id == 2) {
                $where['pd.mini_shop_id'] = 0;
            }

            if ($end_time && $start_time) {
                $where['pd.add_time'] = array('between', array($start_time, $end_time));
            } elseif (empty($end_time) && $start_time) {
                $time = strtotime($start_time);
                $time2 = $time + 24 * 60 * 60;
                $time2 = date('Y-m-d H:i:s', $time2);
                $where['pd.add_time'] = array('between', array($time, $time2));
            }
            //$where['m.machine_type'] = 1;
            $where['pd.ctm_id'] = $ctm_id;
            $orderList = Db::table('pay_order_detail')->alias('pd')
                    ->join(['pay_order_goods' => 'pg'], 'pg.pay_order_id = pd.id', 'left')
                    ->join('store s', 's.id = pd.store_id', 'left')
                    ->join('custom c', 'c.id=pd.ctm_id', 'left')
                    ->join(['brand_info' => 'b'], 'b.id = pd.brand_id', 'left')
                    //->join(['da_machine_store_relation' => 'm'], 'm.store_id = pd.store_id')
                    ->where($where)
                    ->field('pd.id,pd.order_id,pg.name,pg.goods_icon,s.store_name,pd.add_time create_time,c.custom_name,c.custom_phone,pd.pay_money,pd.status,b.name brand_name')
//                ->group('o.id')
                    ->paginate($perPage);
        }
        return $this->ajaxReturn(0, '查询成功', $orderList);
    }

    /*
     * 用户关注的美容师列表
     * author xfwed 
     * date 2019.12.25
     */

    public function ctmCollectStaffList() {
        $id = input('param.id');

        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        //关注的美容师
        $list = CtmCollect::alias('c')
                ->join('staff s', 's.id = c.pro_id', 'left')
                ->where('c.ctm_id', '=', $id)
                ->where('c.type', '=', 2)
                ->field('s.id,s.staff_name,s.nickname,s.telephone,s.cert_img,s.add_time,s.is_job')
                ->paginate($perPage);
        foreach ($list as $k => $v) {
            $list[$k]['serNum'] = Yuyue::where('staff_id', $v['id'])->where('status', 'in', '4,7,8')->count();
        }

        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 用户喜欢的店铺
     * author xfwed 
     * date 2019.12.25
     */

    public function ctmCollectStoreList() {
        $id = input('param.id');

        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        //关注的店铺
        $list = CtmCollect::alias('c')
                ->join('store s', 's.id = c.pro_id', 'left')
                ->join('prov_city_area p', 'p.id = s.city', 'left')
                ->where('c.ctm_id', '=', $id)
                ->where('c.type', '=', 3)
                ->field('s.id,s.store_name,s.store_keeper,s.store_phone,s.logoimg,s.city,s.auditstore,s.add_time,p.name')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 用户收藏的项目
     * author xfwed 
     * date 2019.12.25
     */

    public function ctmCollectItemList() {
        $id = input('param.id');

        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        //关注的店铺
        $list = CtmCollect::alias('c')
                ->join('item i', 'i.id = c.pro_id', 'left')
                ->join('item_class ic', 'ic.id = i.item_type', 'left')
                ->where('c.ctm_id', '=', $id)
                ->where('c.type', '=', 1)
                ->field('i.id,i.item_name,i.item_img,i.item_price,ic.name,c.money,c.add_time')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 用户零钱/代金券
     * author xfwed 
     * date 2019.12.25
     */

    public function ctmCollectCunList() {
        $id = input('param.id');

        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        //代金券明细
        $cunList = CtmCun::alias('cc')
                ->join('coupon c', 'c.id=cc.cun_id', 'left')
                ->where('cc.ctm_id', $id)
                ->field('c.id,c.name,c.money,cc.cun_id,cc.add_time,cc.end_time,cc.employ')
                ->paginate($perPage);
        foreach ($cunList as $k => $v) {
            if ($v['employ'] == 1) {
                $orderRow = Order::where('cun_id', $v['cun_id'])->field('ord_num,pay_time')->find();
                $cunList[$k]['ord_num'] = $orderRow['ord_num'];
                $cunList[$k]['pay_time'] = $orderRow['pay_time'];
            } else {
                $cunList[$k]['ord_num'] = NULL;
                $cunList[$k]['pay_time'] = NULL;
            }
        }
        $data['cunList'] = $cunList;
        //流水记录
        $flowList = CtmFlow::where('ctm_id', $id)
                ->field('id,item_name,type,pro_type,money,add_time')
                ->order('id desc')
                //->paginate($perPage);
                ->paginate($perPage,'',['var_page'=>'page1'])
                ->appends(['ctm_id'=>$id]);
        $data['flowList'] = $flowList;
        return $this->ajaxReturn(0, '查询成功', $data);
    }
    
    /*
     * 用户服务过的店铺
     * author xfwed 
     * date 2020.04.16
     */

    public function ctmSerStoreList() {
        $id = input('param.id');

        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        //服务过的店铺
        $list = Yuyue::alias('y')
                ->join('store s','s.id = y.store_id','left')
                ->join('staff sf','sf.id = y.staff_id','left')
                ->where('y.status','in','4,7,8')
                ->where('y.ctm_id',$id)
                ->field('s.id,s.store_name,s.store_keeper,s.store_phone,s.logoimg,y.staff_time,sf.staff_name')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 用户服务过的美容师
     * author xfwed 
     * date 2020.04.16
     */

    public function ctmSerStaffList() {
        $id = input('param.id');

        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        //服务过的店铺
        $list = Yuyue::alias('y')
                ->join('store st','st.id = y.store_id','left')
                ->join('staff s','s.id = y.staff_id','left')
                ->where('y.status','in','4,7,8')
                ->where('y.ctm_id',$id)
                ->field('s.id,s.staff_name,s.nickname,s.telephone,s.cert_img,s.add_time,s.is_job,st.store_name,y.staff_time')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 我的小店订单
     * 日期 2020-05-12
     * 待付款    pay_order_detail 表status=1
     * 交易完成  pay_order_detail 表status=2,3,4
     * 已结算    pay_order_detail 表status=4  and   ai_ctm_flow"用户流水表" 有数据
     * 退款      pay_order_detail 表status=5,6
     * 
     */

    public function smallStoreOrderList() {
        $rule = [
            'ctm_id' => 'require',
            'type' => 'require',
            'status' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:0',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
            'type.require' => '分类必须',
            'status.require' => '状态必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '页数为数字',
            'page.egt' => '页数最小为0',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        $type = $all['type'];//1商品订单 2特权订单
        $status = $all['status']; //0全部 1待付款 2交易完成 3已结算 4退款
        if($type == 1){
            if($status == 1){
                $where['d.status'] = 1;
            }elseif ($status == 2) {
                $where['d.status'] = ['in','2,3,4'];
                $where['og.is_refund'] = ['notin','1,2'];
                $a = "(SELECT COUNT(`id`) FROM `ai_ctm_flow` WHERE `ctm_id`=" . $ctm_id . " and `pro_type`=9 and `order_id`=d.id) = 0";
                //$where["(SELECT COUNT(`id`) FROM `ai_ctm_flow` WHERE `ctm_id`=" . $ctm_id . " and `pro_type`=9 and `order_id`=og.id)"] = 0;
            }elseif ($status == 3) {
                $where['d.status'] = 4;
                $where['og.is_refund'] = ['notin','1,2'];
                $a = "(SELECT COUNT(`id`) FROM `ai_ctm_flow` WHERE `ctm_id`=" . $ctm_id . " and `pro_type`=9 and `order_id`=d.id) = 1";
                //$where["(SELECT COUNT(`id`) FROM `ai_ctm_flow` WHERE `ctm_id`=" . $ctm_id . " and `pro_type`=9 and `order_id`=og.id)"] = 1;
            }elseif ($status == 4) {
                //$where['d.status'] = ['in','5,6'];
                $where['og.is_refund'] = ['in','1,2'];
            }
        }elseif ($type == 2) {
            if($status){
                $where['o.status'] = $status;
            }
        }
        
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;

        $a = isset($a) ? $a : '';
        $where = isset($where) ? $where : [];
        if($type == 1){
            //$where['d.mini_shop_id'] = $ctm_id;
            $list = Db::table('pay_order_detail')->alias('d')
                    ->join(['pay_order' => 'o'], 'o.order_id = d.parent_order_id', 'left')
                    ->join(['pay_order_goods' => 'og'], 'og.pay_order_id = d.id', 'left')
                    ->join(['brand_info' => 'b'], 'b.id = d.brand_id', 'left')
                    ->join('custom c', 'c.id = d.ctm_id', 'left')
                    ->where($where)
                    ->where($a)
                    ->where("d.mini_shop_id=".$ctm_id." OR d.store_id IN (SELECT smallshop_otherid FROM ai_custom WHERE id = ".$ctm_id." AND smallshop_type=2)")
                    ->order('d.add_time desc')
                    ->field('c.custom_name,c.custom_photo,d.order_id,d.parent_order_id,d.status,d.add_time,d.mini_shop_id,d.ctm_id,b.name,d.id,og.goods_icon,og.name goodsname,og.price,og.goods_detail_name,og.goods_detail_id,og.is_refund,o.pay_money,o.balance_money')
                    ->paginate($perPage)
                    ->each(function ($item,$key) use ($ctm_id){
                        if($item['is_refund']==1){
                            $item['status'] = 5; //退款中
                        }
                        if($item['is_refund']==2){
                            $item['status'] = 6; //已退款
                        }
                        if ($item['status'] == 4) {
                            $ctmFlowRow = CtmFlow::where('ctm_id', $ctm_id)
                                    ->where('pro_type', 9)
                                    ->where('order_id', $item['id'])
                                    ->find();
                            if ($ctmFlowRow) {
                                $item['status'] = 7; //已结算
                            }
                        }
                        if($item['ctm_id'] == $item['mini_shop_id']){//自己买自己小店的没有佣金
                            $item['promoteIncome'] = 0;
                        }  else {
                            $leaveFactoryPrice = Db::table('goods_info_detail')->where('id',$item['goods_detail_id'])->value('leaveFactoryPrice');
                            $item['promoteIncome'] = ($item['price']- $leaveFactoryPrice)* 0.4;
                        }
                        return $item;
                    });
            return $this->ajaxReturn(0, 'success', $list);
        }elseif ($type == 2) {
            $where['o.mini_shop_id'] = $ctm_id;
            $where['o.type'] = 4;
            $list = Order::alias('o')
                    ->join('custom c', 'c.id = o.ctm_id', 'left')
                    ->where($where)
                    ->order('o.add_time desc')
                    ->field('c.custom_name,c.custom_photo,o.ord_num,o.add_time,o.pay_money,o.balance_money,o.status')
                    ->paginate($perPage);
            foreach ($list as $k => $v){
                $list[$k]['goods_icon'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/special.png';
                $list[$k]['goodsname'] = '99特权';
                $list[$k]['promoteIncome'] = 9900;
            }
            return $this->ajaxReturn(0, 'success', $list);
        }
    }
    
    /*
     * 用户流水（提现审核用）
     * 日期 2020-06-23
     * author xfwed
     * 
     */

    public function ctmFlowUseTixian() {
        $rule = [
            'ctm_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        
        $ctmRow = Custom::where('id',$ctm_id)->find();
        $shouru = CtmFlow::where('ctm_id',$ctm_id)->where('type',1)->sum('money');
        $zhichu = CtmFlow::where('ctm_id',$ctm_id)->where('type',2)->sum('money');
        $real_money = $ctmRow['real_money'];//用户表余额
        $flowSheng = bcsub($shouru,$zhichu,2);//流水表
        $data['shouru'] = $shouru;
        $data['zhichu'] = $zhichu;
        $data['flowSheng'] = $flowSheng;
        $status = '账号正常';
        if($real_money != $flowSheng){
          $status = '账号异常';  
        }
        if($flowSheng < 0){
            $status = '账号异常'; 
        }
        $data['status'] = $status;
        $list = CtmFlow::where('ctm_id',$ctm_id)
                ->order('id desc')
                ->paginate($perPage);
        $data['list'] = $list;
        return $this->ajaxReturn(0, 'success', $data);
    }
    
    /*
     * 我的伙伴
     * author xfwed 
     * date 2020.09.02
     */

    public function myCully() {
        $rule = [
            'ctm_id' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:0',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '页数为数字',
            'page.egt' => '页数最小为0',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return $this->ajaxReturn(1, $error, null);
        }
        
        $ctm_id = $all['ctm_id'];
        $perPage = isset($all['perPage']) ? $all['perPage'] : 10;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        
        $where['smallshop_invite_ctmid'] = $ctm_id;
        $where['is_smallshop'] = 1;
        
        $list = Custom::where($where)
                ->order('smallshop_time desc')
                ->field('id,custom_name,custom_photo,smallshop_time')
                ->paginate($perPage);
        $data['num'] = Custom::where($where)->count();
        foreach ($list as $k => $v){
            $id = $v['id'];
            $specialMoney = CtmFlow::alias('f')
                    ->join('order o','o.id = f.order_id','left')
                    ->where('f.pro_type','=',8)
                    ->where('f.ctm_id',$ctm_id)
                    ->where('o.ctm_id',$id)
                    ->sum('f.money');
            $goodsMoney = CtmFlow::alias('f')
                    ->join(['pay_order_detail'=>'o'],'o.id = f.order_id','left')
                    ->where('f.pro_type','=',9)
                    ->where('f.ctm_id',$ctm_id)
                    ->where('o.ctm_id',$id)
                    ->sum('f.money');        
            $list[$k]['money'] = $specialMoney + $goodsMoney;
        }
        $data['list'] = $list;
        return $this->ajaxReturn(0, 'success', $data);
        
    }
    /*
     * 伙伴详情
     * author xfwed 
     * date 2020.09.02
     */

    public function myCullyInfo() {
        $rule = [
            'ctm_id' => 'require',//邀请者id
            'id' => 'require',//被邀请者id
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:0',
        ];
        $msg = [
            'ctm_id.require' => '邀请者id必须',
            'id.require' => '被邀请者id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '页数为数字',
            'page.egt' => '页数最小为0',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return $this->ajaxReturn(1, $error, null);
        }
        
        $ctm_id = $all['ctm_id'];
        $id = $all['id'];
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        $offset = ($page - 1) * $perPage;
        $ctmRow = Custom::where('id',$id)->field('id,custom_name,custom_photo,smallshop_time')->find();

        $specialMoney = CtmFlow::alias('f')
                ->join('order o','o.id = f.order_id','left')
                ->where('f.pro_type','=',8)
                ->where('f.ctm_id',$ctm_id)
                ->where('o.ctm_id',$id)
                ->sum('f.money');
        $goodsMoney = CtmFlow::alias('f')
                ->join(['pay_order_detail'=>'o'],'o.id = f.order_id','left')
                ->where('f.pro_type','=',9)
                ->where('f.ctm_id',$ctm_id)
                ->where('o.ctm_id',$id)
                ->sum('f.money');    
        $ctmRow['money'] = $specialMoney + $goodsMoney;    
        $data['ctmRow'] = $ctmRow;
        $matSql = CtmFlow::alias('f')
                ->join('order o','o.id = f.order_id','left')
                ->where('f.pro_type','=',8)
                ->where('f.ctm_id',$ctm_id)
                ->where('o.ctm_id',$id)
                ->field('f.money,f.pro_type,o.pay_money,o.balance_money,o.ord_num,o.add_time,"" name,"" goods_icon')
                ->buildSql();
        
        $list = CtmFlow::alias('f')
                ->join(['pay_order_detail'=>'o'],'o.id = f.order_id','left')
                ->join(['pay_order_goods'=>'d'],'d.pay_order_id = o.id','left')
                ->where('f.pro_type','=',9)
                ->where('f.ctm_id',$ctm_id)
                ->where('o.ctm_id',$id)
                ->field('f.money,f.pro_type,o.pay_money,"" balance_money,o.order_id ord_num,o.add_time,d.name,d.goods_icon')
                ->limit($offset, $perPage)
                ->union($matSql, true)
                ->select();
        
        
         $num1 = CtmFlow::alias('f')
                ->join('order o','o.id = f.order_id','left')
                ->where('f.pro_type','=',8)
                ->where('f.ctm_id',$ctm_id)
                ->where('o.ctm_id',$id)
                ->count();
        $num2 = CtmFlow::alias('f')
                ->join(['pay_order_detail'=>'o'],'o.id = f.order_id','left')
                ->join(['pay_order_goods'=>'d'],'d.pay_order_id = o.id','left')
                ->where('f.pro_type','=',9)
                ->where('f.ctm_id',$ctm_id)
                ->where('o.ctm_id',$id)
                ->count();
        $data['num'] = $num1 + $num2;
       
        foreach ($list as $k => $v){
           if($v['pro_type'] == 8){
               $list[$k]['name'] = "99特权卡";
               $list[$k]['goods_icon'] = 'http://47.104.83.107/images/kkmimg/kkmai/default/special.png';
           } 
        }
        $data['list'] = $list;
        return $this->ajaxReturn(0, 'success', $data);
        
    }
    
    /*
     * 小店账单
     * author xfwed 
     * date 2020.09.04
     */

    public function smallStoreBillList() {
        $rule = [
            'ctm_id' => 'require',
            'type' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:0',
        ];
        $msg = [
            'ctm_id.require' => '小店id必须',
            'type.require' => '分类必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '页数为数字',
            'page.egt' => '页数最小为0',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return $this->ajaxReturn(1, $error, null);
        }
        
        $ctm_id = $all['ctm_id'];
        $type = $all['type'];//0.全部 1.待入账 2.入账中 3.已入账
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
       
        $ApiPath8891 = config('appconfig.ApiPath');
        return $relust = Utils::curlPost($ApiPath8891 . '/smallshop/orderList/'.$ctm_id.'/'.$type.'/'.$page.'/'.$perPage);
        return $this->ajaxReturn(0, 'success', $relust);
        
    }
    
    /*
     * 我的推广顾客
     * author xfwed
     * date 2021.04.06
     * 此接口上线后 myCtm作废
     */

    public function myCtm() {
        $rule = [
            'staff_id' => 'require',
            'type' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'type.require' => '分类必须',
            'staff_id.require' => '美容师id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $type = $all['type']; //1专属特权2推广顾客3粉丝4足迹5其他(服务完成的客户)
        $staff_id = $all['staff_id'];
        $store_id = input('param.store_id');
        $tab = input('param.tab');//推广顾客 1未消费 2已消费
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        $offset = ($page - 1) * $perPage;
        //tab数量
        //1专属特权
        $specialNum = Order::where('staff_id', $staff_id)//专属特权
                ->where('status', 2)
                ->where('type', 4)
                ->group('ctm_id')
                ->count();
        $data['specialNum'] = $specialNum;
        //2推广顾客
        $spreadNum = Custom::where('status',2)
                ->where('source_type',2)
                ->where('source_id',$staff_id)
                ->count();
        //2推广顾客 1未消费 
        $spreadNoExpenseNum = Custom::alias('c')
                ->where('status',2)
                ->where('source_type',2)
                ->where('source_id',$staff_id)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `ctm_id`=c.id and `status`=2 and `pay_money`>0) = 0")
                ->group('c.id')
                ->count();
        //2推广顾客 2已消费
        $spreadExpenseNum = Custom::alias('c')
                ->where('status',2)
                ->where('source_type',2)
                ->where('source_id',$staff_id)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `ctm_id`=c.id and `status`=2 and `pay_money`>0) > 0")
                ->group('c.id')
                ->count();
        $data['spreadNum'] = $spreadNum;
        $data['spreadNoExpenseNum'] = $spreadNoExpenseNum;
        $data['spreadExpenseNum'] = $spreadExpenseNum;
        //3粉丝
        $fans = CtmCollect::where('type', 2)
                ->where('pro_id', $staff_id)
                ->count();
        $data['fans'] = $fans;
        //4足迹
        $footmark = StaffCtm::where('staff_id',$staff_id)
                ->group('ctm_id')
                ->count();
        $data['footmark'] = $footmark;
        //5其他(服务完成的客户)
        $otherNum = Yuyue::where('staff_id',$staff_id)
                ->where('status','in','4,7,8')
                ->group('ctm_id')
                ->count();
        $data['otherNum'] = $otherNum;
        
        //列表  1专属特权2推广顾客3粉丝4足迹5其他(服务完成的客户)
        if($type == 1){
            $list = Order::where('staff_id', $staff_id)//专属特权
                ->where('status', 2)
                ->where('type', 4)
                ->group('ctm_id')
                //->limit($offset, $perPage)
                ->order('pay_time desc')
                ->field('ctm_id')
                ->paginate($perPage);
        }elseif ($type == 2) {
            if($tab == 1){
               $list = Custom::alias('c')
                    ->where('status',2)
                    ->where('source_type',2)
                    ->where('source_id',$staff_id)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `ctm_id`=c.id and `status`=2 and `pay_money`>0) = 0")
                    ->group('c.id')
                    //->limit($offset, $perPage)
                    ->order('add_time desc')
                    ->field('id ctm_id')
                    ->paginate($perPage);
            }elseif ($tab ==2) {
                $list = Custom::alias('c')
                    ->where('status',2)
                    ->where('source_type',2)
                    ->where('source_id',$staff_id)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `ctm_id`=c.id and `status`=2 and `pay_money`>0) > 0")
                    ->group('c.id')
                    //->limit($offset, $perPage)
                    ->order('add_time desc')
                    ->field('id ctm_id')
                    ->paginate($perPage);
            }
            
        }elseif ($type == 3) {
            $list = CtmCollect::where('type', 2)
                ->where('pro_id', $staff_id)
                //->limit($offset, $perPage)
                ->order('add_time desc')
                ->field('ctm_id')
                ->paginate($perPage);
        }elseif ($type == 4) {
            $list = StaffCtm::where('staff_id',$staff_id)
                ->group('ctm_id')
                //->limit($offset, $perPage)
                ->order('add_time desc')
                ->field('ctm_id')
                ->paginate($perPage);
        }elseif ($type == 5) {
            $list = Yuyue::where('staff_id',$staff_id)
                ->where('status','in','4,7,8')
                ->group('ctm_id')
                //->limit($offset, $perPage)
                ->order('staff_end_time desc')
                ->field('ctm_id')
                ->paginate($perPage);
        }
        
        foreach ($list as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $list[$k]['note'] = $notename['note'];
                $list[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //用户是否关注了美容师
                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
                        ->where('type', 2)
                        ->where('pro_id', $staff_id)
                        ->find();
                if ($ctmCollectRow) {
                    $list[$k]['is_focus'] = 1; //1是2否关注了我
                } else {
                    $list[$k]['is_focus'] = 2;
                }
                //沟通信息
                $list[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->order('add_time desc')
                        ->value('add_time');

                //是否是通过美容师开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('staff_id', $staff_id)
                        ->find();
                if ($orderRow) {
                    $list[$k]['special'] = 1; //1是2否通过我购买的
                } else {
                    $list[$k]['special'] = 2;
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $list[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $list[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $list[$k]['is_medical'] = $is_medical;
                $list[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $list[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $list[$k]['custom_name'] = $ctmRow['custom_name'];
                $list[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $list[$k]['vip_status'] = $ctmRow['vip_status'];
                $list[$k]['source_type'] = $ctmRow['source_type'];
                $list[$k]['source_id'] = $ctmRow['source_id'];
                
                if($ctmRow['source_type'] == 0){
                    $sourceName = '';
                }elseif ($ctmRow['source_type'] == 1) {
                    $sourceName = '平台引流';
                }elseif ($ctmRow['source_type'] == 2) {
                    $staffName = Staff::where('id',$ctmRow['source_id'])->value('staff_name');
                    $sourceName = '美容师'.$staffName;
                }elseif ($ctmRow['source_type'] == 3) {
                    $StoreRow = Store::where('id',$ctmRow['source_id'])->field('store_name,city')->find();
                    $cityName = ProvCityArea::where('id',$StoreRow['city'])->value('name');
                    $sourceName = $cityName.'的'.$StoreRow['store_name'];
                }elseif ($ctmRow['source_type'] == 4) {
                    $sourceName = '小店转介绍';
                }
                $list[$k]['sourceName'] = $sourceName;
                //查询续费年数
                $specialNum = CtmSpecial::where('ctm_id',$v['ctm_id'])->count();
                $list[$k]['renewYears'] = $specialNum;
                
            }
            $data['list'] = $list;
        return $this->ajaxReturn(0, 'success', $data);
    }
     /*
     * 踢下线
     * author xfwed 
     * date 2020.10.09
     */

    public function offline() {
        $rule = [
            'ctm_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return $this->ajaxReturn(1, $error, null);
        }
        
        $ctm_id = $all['ctm_id'];
       
        $ApiPath = config('appconfig.ApiPath');
        return $relust = Utils::curlPost($ApiPath . '/customer/logout?ctmId='.$ctm_id);
    }
    
    /*
     * 封禁账户
     * author xfwed 
     * date 2020.10.09
     */

    public function disable() {
        $rule = [
            'ctm_id' => 'require',
            'time' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
            'time.require' => 'time必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return $this->ajaxReturn(1, $error, null);
        }
        
        $ctm_id = $all['ctm_id'];
        $time = $all['time'];
       
        $ApiPath = config('appconfig.ApiPath');
        return $relust = Utils::curlPost($ApiPath . '/customer/disable?ctmId='.$ctm_id.'&time='.$time);
    }
    /*
     * 封禁账户列表
     * author xfwed 
     * date 2020.10.09
     */

    public function disableList() {
        $ApiPath = config('appconfig.ApiPath');
        $relust = Utils::curlPost($ApiPath . '/customer/queryDisableList/');
        $relust = json_decode($relust,true);
//        print_r($relust);die;
        if($relust['code'] == 0){
            $data = $relust['data'];
            $list = [];
            foreach ($data as $k=>$v){
                $customRow = Custom::where('id',$v['ctmId'])
                    ->field('id,custom_name,custom_phone,vip_status,consume_money,real_money,tx_money,add_time,longitude,latitude,warning_level,exclusive_store_id,exclusive_staff_id,ser_num,sell_time,visit_num,red_money')
                    ->find();
                $seconds = $v['time'];
                if($seconds == -1){
                    $time = '永久封禁';
                }  else {
                    if( $seconds>3600 ){
                        if( $seconds>24*3600 ){
                            $days        = (int)($seconds/86400);
                            $days_num    = $days."天";
                            $seconds    = $seconds%86400;//取余
                        }  else {
                            $days_num = "0天";
                        }
                        $hours = intval($seconds/3600);
                        $minutes = $seconds%3600;//取余下秒数
                        $time = $days_num.$hours."小时".gmstrftime('%M分钟%S秒', $minutes);
                    }else{
                        $time = gmstrftime('%H小时%M分钟%S秒', $seconds);
                    }
                }
                
                $customRow['time'] = $time;
                $list[$k] = $customRow;
            }
            return Utils::ajaxReturn(0, '查询成功', $list);
        }  else {
            return Utils::ajaxReturn(0, '查询失败', null);
        }
        
    }
    
    /*
     * 解除封禁
     * author xfwed 
     * date 2020.10.09
     */

    public function untieDisable() {
        $rule = [
            'ctm_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return $this->ajaxReturn(1, $error, null);
        }
        
        $ctm_id = $all['ctm_id'];
       
        $ApiPath = config('appconfig.ApiPath');
        return $relust = Utils::curlPost($ApiPath . '/customer/untieDisable?ctmId='.$ctm_id);
    }

}
